package com.tongzhou.client;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class TongZhouAction extends ActionSupport{
	private HttpServletRequest request;
	private String json;
	private String method;
	public String execute(){
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletResponse response2 = ServletActionContext.getResponse();
		request=ServletActionContext.getRequest();
		response.setContentType("text/html;charset=UTF-8");
        PrintWriter out;
		try {
			
			JSONObject jsonObject = JSONObject.fromObject( json );
			String returnStr="";
			if(method!=null){
				if(method.equals("1")){
					//登陆接口
					ClientLogin login=new ClientLogin();
					returnStr=login.getInfo(json);
				}else if(method.equals("2")){
					//注册接口
					ClientRegist clientRegist=new ClientRegist();
					returnStr=clientRegist.getInfo(json);
				}else if(method.equals("3")){
					//应用初始化接口
					ClientInit clientInit=new ClientInit();
					returnStr=clientInit.getInfo(json);
				}else if(method.equals("4")){
					Integer type=(Integer) jsonObject.get("type");
					if(type!=null){
						if(type==1){
							//报价接口
							ClientQuot clientQuot=new ClientQuot();
							returnStr=clientQuot.getInfo(json);
						}else if(type==2){
							//报价接口
//							ClientQuot2 clientQuot2=new ClientQuot2();
//							returnStr=clientQuot2.getInfo();
//							request.getRequestDispatcher("/").forward(request, response);
							PrintWriter outp = null;   
							InputStream in = null;
							BufferedInputStream bis = null;
							BufferedOutputStream bos = null;
							try {
								String filedisplay = "excel文件.xls"; 
								response2.setContentType("application/x-msdownload;");
								response2.setHeader("Content-disposition", "attachment; filename="
								  + new String(filedisplay.getBytes("gb2312"), "ISO8859-1"));
								response2.setHeader("Content-Length", String.valueOf(filedisplay));
								response2.setCharacterEncoding("utf-8");
								bis = new BufferedInputStream(new FileInputStream("D:/TEMP/wenjian.xls"));
								bos = new BufferedOutputStream(response2.getOutputStream());
								byte[] buff = new byte[2048];
								int bytesRead;
								while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
									bos.write(buff, 0, bytesRead);
								}
							} catch (Exception e) {
								e.printStackTrace();
							} finally {
						  		if (bis != null){
						  			try {
										bis.close();
									} catch (IOException e) {
										// TODO Auto-generated catch block
										e.printStackTrace();
									}
						  		}
						  		if (bos != null){
						  			try {
										bos.close();
									} catch (IOException e) {
										// TODO Auto-generated catch block
										e.printStackTrace();
									}
						  		}
						  	}
							return null;
						}else if(type==3){
							//报价接口
							ClientQuot3 clientQuot3=new ClientQuot3();
							returnStr=clientQuot3.getInfo(json);
						}
					}
				}else if(method.equals("5")){
					//生成排期接口
					ClientProduct clientProduct=new ClientProduct();
					returnStr=clientProduct.getInfo(json);
				}else if(method.equals("6")){
					//物流查询接口
					ClientQuery clientQuery=new ClientQuery();
					returnStr=clientQuery.getInfo(json);
				}else if(method.equals("7")){
					//物流查询详细接口
					ClientDetail clientDetail=new ClientDetail();
					returnStr=clientDetail.getInfo(json);
				}else if(method.equals("8")){
					//同舟快讯接口
					ClientNews clientNews=new ClientNews();
					returnStr=clientNews.getInfo(json);
				}else if(method.equals("9")){
					//促销接口
					ClientPromotion clientPromotion=new ClientPromotion();
					returnStr=clientPromotion.getInfo(json);
				}else if(method.equals("10")){
					//升级、更新接口
					ClientUpdate clientUpdate=new ClientUpdate();
					returnStr=clientUpdate.getInfo(json);
				}else if(method.equals("11")){
					//留言接口
					ClientMsg clientMsg=new ClientMsg();
					returnStr=clientMsg.getInfo(json);
				}
			}
			out = response.getWriter();
	        out.print(returnStr);
	        out.flush();
	        out.close();
	        return null;
		} catch (Exception e) {
			e.printStackTrace();
		}
		try {
			PrintWriter out2 = response.getWriter();
			out2.print("{'success':'0','msg':'系统出错'}");
	        out2.flush();
	        out2.close();
		} catch (Exception e1) {
			e1.printStackTrace();
		}
        return null; 
	}
	public String getJson() {
		return json;
	}
	public void setJson(String json) {
		this.json = json;
	}
	public String getMethod() {
		return method;
	}
	public void setMethod(String method) {
		this.method = method;
	}
	
}
